/*
 * MailMessageResource.cpp
 *
 *  Created on: 2013. 11. 15.
 *      Author: mega@gamevil.com
 */

#include "../NUtil/Pool.h"
#include "MailMessageResource.h"

namespace ProjectZ { namespace NResource {

MailMessageResource::MailMessageResource()
{

}

MailMessageResource::~MailMessageResource()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ::ProjectZ::NUtil::del_second_ptr<MAILMESSAGETREE::value_type>);
}

void
MailMessageResource::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ::ProjectZ::NUtil::del_second_ptr<MAILMESSAGETREE::value_type>);
	clsFlyTree.clear();
}

bool
MailMessageResource::LoadResource()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("select * from resource_mail_message");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		Flyweight * pFly = new Flyweight;
		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_MESSAGE_TYPE >> pFly->_MESSAGE;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_MESSAGE_TYPE] = pFly;
	}

	return true;
}

MailMessageResource::Flyweight *
MailMessageResource::GetFly(MailMessageType eType)
{
	int nType = static_cast<int>(eType);

	auto iter = clsFlyTree.find(nType);
	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

const char *
MailMessageResource::GetMessageTradeItemBuy()
{
	/*
	 * 거래소 구입 아이템 지급
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_TRADE_ITEM_BUY);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageTradeItemCancel()
{
	/*
	 * 거래서 등록 아이템 취소
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_TRADE_ITEM_CANCEL);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageTradeItemSoldout()
{
	/*
	 * 거래소 등록 아이템 판매대금
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_TRADE_ITEM_SOLDOUT);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageFriendshipReward()
{
	/*
	 * 우정보물함 보상
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_FRIENDSHIP_BOX_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageDailyReward()
{
	/*
	 * 일일(연속) 접속 보상
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_DAILY_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageWeeklyRankingReward()
{
	/*
	 * 주간 랭킹 보상
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_LAST_WEEK_RANKING_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageMonthChargingEventReward()
{
	/*
	 * 월 구매 이벤트
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_MONTH_CHARGING_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageFairyPackage()
{
	/*
	 * 페어리 패키지 지급
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_FAIRY_PACKAGE);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageBattlepetPackage()
{
	/*
	 * 전투펫 패키지 지급
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_BATTLEPET_PACKAGE);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessagePVPWinReward()
{
	/*
	 * PVP 승리 보상
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_PVP_WIN_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageItemGachya()
{
	/*
	 * 아이템 뽑기
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_ITEM_GACHYA_RESULT);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageDungeonReward(int gachyaType)
{
	/*
	 * 던전 보상
	 * gachyaType : 0(일반카드), 1(젠카드)
	 */

	MailMessageType eType = MailMessageType::__NONE__;
	switch (gachyaType)
	{
	case 0: eType = MailMessageType::EM_DUNGEON_REWARD_NORMAL_ITEM; break;
	case 1: eType = MailMessageType::EM_DUNGEON_REWARD_ZEN_ITEM; break;
	}

	if (eType == MailMessageType::__NONE__)
	{
		return emptyMessage.c_str();
	}

	Flyweight * pFly = GetFly(eType);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageFreeChargeReward()
{
	/*
	 * CPI 보상
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_FREE_CHARGE_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageGiftZen()
{
	/*
	 * 젠 선물
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_GIFT_ZEN);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageConnectEvent()
{
	/*
	 * 접속 이벤트 보상
	 */
	Flyweight * pFly = GetFly(MailMessageType::EM_CONNECT_EVENT);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageWorldBossReward()
{
	Flyweight * pFly = GetFly(MailMessageType::EM_WORLDBOSS_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

const char *
MailMessageResource::GetMessageDailyMissionReward()
{
	Flyweight * pFly = GetFly(MailMessageType::EM_DAILY_MISSION_REWARD);
	if (NULL == pFly)
	{
		return emptyMessage.c_str();
	}

	return pFly->_MESSAGE.c_str();
}

} /*NResource*/ } /*ProjectZ*/
